package cn.jhc.teachingquality.net.teaching;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;

import org.jsoup.nodes.Document;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jhc.teachingquality.Global;
import cn.jhc.teachingquality.bean.Student;
import cn.jhc.teachingquality.bean.TeachingLog;
import cn.jhc.teachingquality.net.TeachingQualityRequest;

/**
 * 保存教学日志。
 * Created by luyanfei on 16-4-16.
 */
public class SaveTeachingLogRequest extends TeachingQualityRequest<String> {
    private static final String SAVE_TEACHING_LOG_URL =
            Global.URL_PREFIX + "/pjJxrzAction!save.shtml";

    private TeachingLog teachingLog;
    private List<Student> studentList;
    private String contentOfLesson;

    public SaveTeachingLogRequest(Map<String,Object> formData, Response.Listener<String> listener) {
        super(Method.POST, SAVE_TEACHING_LOG_URL, listener);
        this.teachingLog = (TeachingLog) formData.get("teaching_log_object");
        this.studentList = (List<Student>) formData.get("students_list");
        this.contentOfLesson = (String) formData.get("content_of_lesson");
    }

    @Override
    protected String handleResponse(Document document) {
        return null;
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String,String> map = new HashMap<>();
        map.put("jcKcxx.id", teachingLog.getHiddenId());
        map.put("pjJxrz.skss", teachingLog.getLessonCount() + "");
        map.put("pjJxrz.sknr", contentOfLesson);
        map.put("jsonString", buildJsonString());
        for(Student student : studentList){
            map.put("xsid_" + student.getId(), student.getRollStatus().ordinal() + "");
        }
        return map;
    }

    private String buildJsonString(){
        StringBuilder sb = new StringBuilder("undefined;undefined");
        for(Student student : studentList){
            sb.append(",").append(student.getId())
                    .append(";").append(student.getRollStatus().ordinal());
        }
        return sb.toString();
    }
}
